package black.market.weather;

import java.sql.Date;
import java.sql.Time;
import java.util.ArrayList;

import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import black.market.main.BlackPublic;
import black.market.main.BlackPublic.Action;
import black.market.util.Log;

public class KmaInfoDAO {
	public static final String TAG = "KmaInfoDAO";
	private ContentResolver mContentResolver = null;
	private Context mContext = null;

	public KmaInfoDAO(Context context) {
		super();
		this.mContext = context;
		this.mContentResolver = mContext.getContentResolver();
	}

	/**
	 * @param u
	 *            (KmaWeatherCP Table Uri)
	 * 
	 * @return
	 */
	public boolean isKmaDatabase_Null(Uri u) {
		Log.d(TAG, "isKmaDatabase_NullCheck(Uri " + u + ")");
		Uri uri = u;
		String[] projection = null;
		String selection = null;
		String[] selectionArgs = null;
		String sortOrder = null;
		Cursor c = mContentResolver.query(uri, projection, selection, selectionArgs, sortOrder);
		int cnt = c.getCount();
		c.close();
		return cnt == 0;
	}

	public String[] getKmaHeaderLastTimeMapArr() {
		Log.d(TAG, "getKmaHeaderLastTimeMap()");
		String[] tm = new String[3];
		Uri uri = WeatherContentProvider.HEADER_URI;
		String[] projection = { WeatherContentProvider.WB_TIMEMAP };
		String selection = null;
		String[] selectionArgs = null;
		String sortOrder = null;
		Cursor c = mContentResolver.query(uri, projection, selection, selectionArgs, sortOrder);
		if (c.moveToFirst()) {
			if (c.isLast()) {
				String stf = c.getString(c.getColumnIndex(WeatherContentProvider.WB_TIMEMAP));
				Log.d(TAG, "getKmaHeaderLastTimeMapArr timeMap -> " + stf);
				tm[0] = stf.substring(0, 4);
				tm[1] = stf.substring(4, 6);
				tm[2] = stf.substring(6, 8);
			}
		}
		if (!c.isClosed()) {
			c.close();
		}
		for (int i = 0; i < tm.length; i++) {
			Log.d(TAG, "getKmaHeaderLastTimeMapArr -> " + tm[i]);
		}
		return tm;
	}

	public long getKmaHeaderLastTimeMapLong() {
		Log.d(TAG, "getKmaHeaderLastTimeMap()");
		Uri uri = WeatherContentProvider.HEADER_URI;
		String[] projection = { WeatherContentProvider.WB_TIMEMAP };
		String selection = null;
		String[] selectionArgs = null;
		String sortOrder = null;
		Cursor c = mContentResolver.query(uri, projection, selection, selectionArgs, sortOrder);
		if (c.moveToFirst()) {
			if (c.isLast()) {
				String stf = c.getString(c.getColumnIndex(WeatherContentProvider.WB_TIMEMAP));
				Log.d(TAG, "getKmaHeaderLastTimeMapArr timeMap -> " + stf);
				Log.d(TAG, "getKmaHeaderLastTimeMapArr timeMap year -> " + Integer.parseInt(stf.substring(0, 4).trim()));
				Log.d(TAG, "getKmaHeaderLastTimeMapArr timeMap mon -> " + Integer.parseInt(stf.substring(4, 6).trim()));
				Log.d(TAG, "getKmaHeaderLastTimeMapArr timeMap date -> " + Integer.parseInt(stf.substring(6, 8).trim()));
				Date d = new Date(Integer.valueOf(stf.substring(0, 4).trim()) - 1900, Integer.valueOf(stf.substring(4, 6).trim()) - 1, Integer.valueOf(stf.substring(6, 8).trim()));
				return d.getTime();
			}
		}
		if (!c.isClosed()) {
			c.close();
		}
		return 0;
	}

	public String getKmaHeaderLastTimeMap() {
		Log.d(TAG, "getKmaHeaderLastTimeMap()");
		String tm = null;
		Uri uri = WeatherContentProvider.HEADER_URI;
		String[] projection = { WeatherContentProvider.WB_TIMEMAP };
		String selection = null;
		String[] selectionArgs = null;
		String sortOrder = null;
		Cursor c = mContentResolver.query(uri, projection, selection, selectionArgs, sortOrder);
		if (c.moveToFirst()) {
			if (c.isLast()) {
				tm = c.getString(c.getColumnIndex(WeatherContentProvider.WB_TIMEMAP));
				Log.d(TAG, "getKmaHeaderTimeMap Last timeMap: " + tm);
			}
		}
		c.close();
		return tm;
	}

	public ArrayList<KmaInfoBean> getKmaBodyCursor(int args) {
		Uri uri = WeatherContentProvider.BODY_URI;
		String[] projection = null;
		String selection = WeatherContentProvider.WB_DAY + "=? and " + WeatherContentProvider.WB_TIMEMAP + "=?";
		String[] selectionArgs = { Integer.toString(args), getKmaHeaderLastTimeMap() }; // 0:today/1:tomorrow/2.afterTomorrow
		String sortOrder = WeatherContentProvider.WB_SEQ + " ASC";
		Cursor c = mContentResolver.query(uri, projection, selection, selectionArgs, sortOrder);
		Log.d(TAG, "getTodayKmaCursor getCount: " + c.getCount());
		ArrayList<KmaInfoBean> kmaInfoList = null;
		if (BlackPublic.checkCursor(c)) {
			kmaInfoList = new ArrayList<KmaInfoBean>();
			do {
				String time = c.getString(c.getColumnIndex(WeatherContentProvider.WB_HOUR));
				String state = c.getString(c.getColumnIndex(WeatherContentProvider.WB_KOR_STATE));
				if (state.equals("눈/비")) {
					state = "눈비";
				}
				String temp = c.getString(c.getColumnIndex(WeatherContentProvider.WB_TEMP));
				String pop = c.getString(c.getColumnIndex(WeatherContentProvider.WB_POP));
				String rain = c.getString(c.getColumnIndex(WeatherContentProvider.WB_RAIN12));
				String snow = c.getString(c.getColumnIndex(WeatherContentProvider.WB_SNOW12));
				String minTemp = c.getString(c.getColumnIndex(WeatherContentProvider.WB_TEMPMIN));
				String maxTemp = c.getString(c.getColumnIndex(WeatherContentProvider.WB_TEMPMAX));
				String reh = c.getString(c.getColumnIndex(WeatherContentProvider.WB_REH));
				String wind_speed = c.getString(c.getColumnIndex(WeatherContentProvider.WB_WINDSPEED));
				String wind_direction = c.getString(c.getColumnIndex(WeatherContentProvider.WB_KOR_WINDDIRECTION));
				kmaInfoList.add(new KmaInfoBean(time, state, temp, pop, rain, snow, minTemp, maxTemp, reh, wind_speed, wind_direction));
				Log.d(TAG, "cursorToStringArrayList Add: " + time + "/" + state + "/" + temp + "/" + pop + "/" + rain + "/" + snow + "/minTemp:" + minTemp + "/maxTemp:" + maxTemp + "/reh:" + reh
						+ "/wind_speed:" + wind_speed + "/wind_direction:" + wind_direction);
			} while (c.moveToNext());
			Log.d(TAG, "cursorToStringArrayList Add: " + kmaInfoList.size());
			c.close();
		} else {
			BlackPublic.errorLog(TAG, "cursorToStringArrayList is null -> sendBroadCast ACTION(KMA_DATA_INTERVAL3H)");
			mContext.sendBroadcast(new Intent(Action.KMA_DATA_INTERVAL3H.toString()));
		}

		if (!c.isClosed()) {
			c.close();
		}
		return kmaInfoList;
	}

	private String[] getTimeMap() {
		Uri uri = WeatherContentProvider.HEADER_URI;
		String[] projection = { WeatherContentProvider.WB_TIMEMAP };
		String selection = null;
		String[] selectionArgs = null;
		String sortOrder = null;
		Cursor c = mContentResolver.query(uri, projection, selection, selectionArgs, sortOrder);
		Log.d(TAG, c.getCount() + "asdasdasd");
		String[] tmp = new String[c.getCount()];
		for (int i = 0; c.moveToNext(); i++) {
			tmp[i] = c.getString(c.getColumnIndex(WeatherContentProvider.WB_TIMEMAP));
		}
		c.close();
		return tmp;
	}

	private String[][] getTimeMap_Id() {
		Uri uri = WeatherContentProvider.BODY_URI;
		String[] projection = { WeatherContentProvider.WB_TIMEMAP, WeatherContentProvider.WB_BODY_ID };
		String selection = null;
		String[] selectionArgs = null;
		String sortOrder = null;
		Cursor c = mContentResolver.query(uri, projection, selection, selectionArgs, sortOrder);
		String[][] tmp = new String[c.getCount()][2];
		for (int i = 0; c.moveToNext(); i++) {
			tmp[i][0] = c.getString(c.getColumnIndex(WeatherContentProvider.WB_BODY_ID));
			tmp[i][1] = c.getString(c.getColumnIndex(WeatherContentProvider.WB_TIMEMAP));
		}
		c.close();
		return tmp;
	}

	public String[][] getHour_Id() {
		Uri uri = WeatherContentProvider.BODY_URI;
		String[] projection = { WeatherContentProvider.WB_HOUR, WeatherContentProvider.WB_BODY_ID };
		String selection = null;
		String[] selectionArgs = null;
		String sortOrder = null;
		Cursor c = mContentResolver.query(uri, projection, selection, selectionArgs, sortOrder);
		String[][] tmp = new String[c.getCount()][2];
		for (int i = 0; c.moveToNext(); i++) {
			tmp[i][0] = c.getString(c.getColumnIndex(WeatherContentProvider.WB_BODY_ID));
			tmp[i][1] = c.getString(c.getColumnIndex(WeatherContentProvider.WB_HOUR));
		}
		c.close();
		return tmp;
	}

	public String[] getHour() {
		Uri uri = WeatherContentProvider.BODY_URI;
		String[] projection = { WeatherContentProvider.WB_HOUR };
		String selection = null;
		String[] selectionArgs = null;
		String sortOrder = null;
		Cursor c = mContentResolver.query(uri, projection, selection, selectionArgs, sortOrder);
		String[] tmp = new String[c.getCount()];
		for (int i = 0; c.moveToNext(); i++) {
			tmp[i] = c.getString(c.getColumnIndex(WeatherContentProvider.WB_HOUR));
		}
		c.close();
		return tmp;
	}

	public void deleteWeather_Head() {
		String[] deleteTimeMapArr = getTimeMap();
		Date d = new Date(System.currentTimeMillis());
		for (int i = 0; i < deleteTimeMapArr.length; i++) {
			Log.d(TAG, deleteTimeMapArr[i]);
			String str = deleteTimeMapArr[i];
			int j = Integer.parseInt(str.substring(6, 8));
			if (d.getDate() > j) {
				Log.d(TAG, "deleteWeather_Head(): true/" + d.getDate() + "/" + str.subSequence(6, 8));
				deleteWeatherHead(deleteTimeMapArr[i]);
			}
		}
	}

	public void deleteWeather_Body() {
		String[][] deleteTimeMapArr = getTimeMap_Id();
		Date d = new Date(System.currentTimeMillis());
		Time t = new Time(System.currentTimeMillis());
		for (int i = 0; i < deleteTimeMapArr.length; i++) {
			String str = deleteTimeMapArr[i][1];
			int j = Integer.parseInt(str.substring(8, 10));
			if (d.getTime() > j) {
				Log.d(TAG, "deleteWeather_Body(): true/" + d.getTime() + "/" + str.subSequence(8, 10));
				deleteWeatherBody(deleteTimeMapArr[i][0]);
			}
		}
	}

	private void deleteWeatherHead(String delTm) {
		Uri uri = WeatherContentProvider.HEADER_URI;
		String where = WeatherContentProvider.WB_TIMEMAP + "=?";
		String[] selectionArgs = { delTm };
		mContentResolver.delete(uri, where, selectionArgs);
	}

	private void deleteWeatherBody(String delTm) {
		Uri uri = WeatherContentProvider.BODY_URI;
		String where = WeatherContentProvider.WB_BODY_ID + "=?";
		String[] selectionArgs = { delTm };
		mContentResolver.delete(uri, where, selectionArgs);
	}
}
